<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .progress {
        padding: 5px;
        border: 1px solid;
      }
      .progress .progressBar {
        height: 10px;
        width: 0px;
        background: gray;
      }
    </style>
  </head>
  <body>
    下载进度
    <progress id="progress" value="0" max="100"></progress>

    <div class="progress">
      <div class="progressBar" id="progressBar"></div>
    </div>

    <script>
      // apifox、REST Client
      // 调用后端接口（函数），需要考虑输入和输出
      const xhr = new XMLHttpRequest();
      xhr.open("GET", "http://127.0.0.1:3000/posts");
      xhr.responseType = "json";
      xhr.onload = function () {
        console.log(xhr.status);
        console.log(xhr.statusText);
        // console.log(JSON.parse(xhr.response));
        console.log(xhr.response);
      };
      xhr.onerror = function () {
        console.log("onerror");
      };
      xhr.onprogress = function (e) {
        let v = 0;
        if (e.total === 0) {
          v = 100;
        } else {
          v = Math.floor((e.loaded / e.total) * 100);
        }

        document.getElementById("progress").value = v;
        document.getElementById("progressBar").style.width = v + "%";
      };
      xhr.send();
    </script>
  </body>
</html>
